<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="商店ID" prop="shopId">
        <el-select
          v-model="queryParams.shopId"
          placeholder="请选择商店ID"
          clearable
        >
          <el-option
            v-for="dict in dict.type.shop_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="食品名字" prop="foodName">
        <el-input
          v-model="queryParams.foodName"
          placeholder="请输入食品名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="限购数量 > = " prop="limitNum">
        <el-input
          v-model="queryParams.limitNum"
          placeholder="请输入限购数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="好评率 > =" prop="praiseRate">
        <el-input
          v-model="queryParams.praiseRate"
          placeholder="请输入好评率"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        >
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:food:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:food:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:food:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:food:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="foodList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商店ID" align="center" prop="shopId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_id" :value="scope.row.shopId"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="分类ID" align="center" prop="categoryId" /> -->
      <el-table-column label="食品名字" align="center" prop="foodName"/>
      <el-table-column label="标签" align="center" prop="tag"/>

      <el-table-column label="描述" align="center" prop="foodDesc"/>
      <el-table-column
        label="食品封面图"
        align="center"
        prop="foodCover"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.foodCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="originPrice"/>
      <el-table-column label="售价" align="center" prop="sellPrice"/>
      <el-table-column label="折扣" align="center" prop="discount"/>
      <el-table-column label="点赞" align="center" prop="likeCount"/>
      <el-table-column label="限购数量" align="center" prop="limitNum"/>
      <el-table-column label="总的销量" align="center" prop="totalSales"/>
      <el-table-column label="月销量" align="center" prop="monthSales"/>
      <el-table-column label="好评率" align="center" prop="praiseRate">
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="foodStatus" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:food:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:food:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商店ID" prop="shopId">
          <el-select
            v-model="form.shopId"
            @change="getCategoryList()"
            placeholder="请选择商店ID"
          >
            <el-option
              v-for="dict in dict.type.shop_id"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类ID" prop="categoryId">
          <!-- <el-input v-model="form.categoryId" placeholder="请输入分类ID" /> -->
          <el-select v-model="form.categoryId" placeholder="请选择分类">
            <el-option
              v-for="category in categoryList"
              :key="category.value"
              :label="category.label"
              :value="parseInt(category.value)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="食品名字" prop="foodName">
          <el-input v-model="form.foodName" placeholder="请输入食品名字"/>
        </el-form-item>
        <el-form-item label="描述" prop="foodDesc">
          <el-input v-model="form.foodDesc" placeholder="请输入描述"/>
        </el-form-item>
        <el-form-item label="食品封面图">
          <image-upload v-model="form.foodCover"/>
        </el-form-item>
        <el-form-item label="标签">
          <el-input v-model="form.tag" placeholder="请输入标签"/>
        </el-form-item>
        <el-form-item label="原价" prop="originPrice">
          <el-input v-model="form.originPrice" placeholder="请输入原价"/>
        </el-form-item>
        <el-form-item label="售价" prop="sellPrice">
          <el-input v-model="form.sellPrice" placeholder="请输入售价"/>
        </el-form-item>
        <el-form-item label="折扣" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入折扣"/>
        </el-form-item>
        <!-- <el-form-item label="点赞" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入点赞" />
        </el-form-item> -->
        <el-form-item label="限购数量" prop="limitNum">
          <el-input v-model="form.limitNum" placeholder="请输入限购数量"/>
        </el-form-item>
        <!-- <el-form-item label="总的销量" prop="totalSales">
          <el-input v-model="form.totalSales" placeholder="请输入总的销量" />
        </el-form-item>
        <el-form-item label="月销量" prop="monthSales">
          <el-input v-model="form.monthSales" placeholder="请输入月销量" />
        </el-form-item> -->
        <!-- <el-form-item label="好评率" prop="praiseRate">
          <el-input v-model="form.praiseRate" placeholder="请输入好评率" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFood,
  getFood,
  delFood,
  addFood,
  updateFood,
} from "@/api/system/food";
import {getCategoryByShopId} from "@/api/system/category";

export default {
  name: "Food",
  dicts: ["shop_id", "food_spec_parent"],
  data() {
    return {
      categoryList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品信息表格数据
      foodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: null,
        foodName: null,
        sellPrice: null,
        limitNum: null,
        totalSales: null,
        monthSales: null,
        praiseRate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopId: [
          {required: true, message: "商店ID不能为空", trigger: "change"},
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getCategoryList() {
      console.log(this.form.shopId);
      getCategoryByShopId(this.form.shopId).then((res) => {
        console.log(res);
        for (let i = 0; i < res.length; i++) {
          const element = res[i];
          this.categoryList.push(element);
        }
      });
    },
    /** 查询菜品信息列表 */
    getList() {
      this.loading = true;
      listFood(this.queryParams).then((response) => {
        this.foodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tag: null,
        shopId: null,
        categoryId: null,
        foodName: null,
        foodDesc: null,
        foodCover: null,
        originPrice: null,
        sellPrice: null,
        discount: null,
        likeCount: null,
        limitNum: null,
        foodSpecId: null,
        totalSales: null,
        monthSales: null,
        praiseRate: null,
        foodStatus: 0,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFood(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFood(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFood(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除菜品信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delFood(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/food/export",
        {
          ...this.queryParams,
        },
        `food_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
